<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Plugin: htmldataprocessor</title>
	<link rel="stylesheet" type="text/css" href="../../test.css" />
	<script type="text/javascript" src="../../../ckeditor.js"></script>
	<script type="text/javascript" src="../../test.js"></script>
	<script type="text/javascript">
	//<![CDATA[

// Load the required plugins and launch the runner.
CKEDITOR.test.deferRunner = true;
CKEDITOR.plugins.load( 'htmldataprocessor' , function()
{
 CKEDITOR.test.runner.run();
} );

var testCase;

CKEDITOR.test.addTestCase( testCase = (function()
{
	// Local references.
	var assert = CKEDITOR.test.assert;
	var doc = new CKEDITOR.dom.document( document );

	// In these tests, we may "reset" the writer rules to avoid it formatting
	// the output, making the assertion easier to the done. We don't need to
	// test formatting features here, so this is ok.
	var getDataProcessor = function()
	{
		var dataProcessor = new CKEDITOR.htmlDataProcessor();
		dataProcessor.writer._.rules = [];
		return dataProcessor;
	};

	/**
	 * IE always returning CRLF for linefeed, so remove it when retrieve pre-formated text from text area.
	 * @param {Object} id
	 */
	function getTextAreaValue( id )
	{
		return CKEDITOR.document.getById( id ).getValue().replace(/\r/gi,'');
	}

	// These tests go far beyond the strict htmlDataProcessor code testing. We
	// are actually testing the entire parsing system here. The combination of
	// htmlParser and htmlWriter inside htmlDataProcessor is useful in this
	// sense.

	return {
		test_toDataFormat_1a : function()
		{
			var element = new CKEDITOR.dom.element.createFromHtml( '<div><p>Test</p></div>' );

			assert.areSame( '<p>Test</p>', getDataProcessor().toDataFormat( element.getHtml() ) );
		},

		test_toDataFormat_1b : function()
		{
			var element = new CKEDITOR.dom.element.createFromHtml( '<div><x:x>Test</x:x></div>' );

			// IE adds the XML namespace tag.
			if ( CKEDITOR.env.ie )
				assert.areSame( '<?xml:namespace prefix="x" /><x:x>Test</x:x>', getDataProcessor().toDataFormat( element.getHtml() ) );
			else
				assert.areSame( '<x:x>Test</x:x>', getDataProcessor().toDataFormat( element.getHtml() ) );
		},

		test_toDataFormat_2a : function()
		{
			var element = new CKEDITOR.dom.element.createFromHtml( '<div><br /><p>Test</p></div>' );

			assert.areSame( '<br /><p>Test</p>', getDataProcessor().toDataFormat( element.getHtml() ) );
		},

		test_toDataFormat_2b : function()
		{
			var element = new CKEDITOR.dom.element.createFromHtml( '<div><x:x></x:x><p>Test</p></div>' );

			// IE adds the XML namespace tag.
			if ( CKEDITOR.env.ie )
				assert.areSame( '<?xml:namespace prefix="x" /><x:x></x:x><p>Test</p>', getDataProcessor().toDataFormat( element.getHtml() ) );
			else
				assert.areSame( '<x:x></x:x><p>Test</p>', getDataProcessor().toDataFormat( element.getHtml() ) );
		},

		test_toDataFormat_3 : function()
		{
			assert.areSame( '<div><x:x><p>Test</p></x:x></div>', getDataProcessor().toDataFormat( '<div><x:x><p>Test</p></div>' ) );
		},

		test_toDataFormat_ticket_2774 : function()
		{
			var element = new CKEDITOR.dom.element.createFromHtml( '<div><P class=MsoNormal><B><I><SPAN lang=EN-US><o:p>Test</o:p></SPAN></I></B></P></div>' );

			// IE adds the XML namespace tag.
			if ( CKEDITOR.env.ie )
				assert.areSame( '<p class="MsoNormal"><b><i><span lang="EN-US"><?xml:namespace prefix="o" /><o:p>Test</o:p></span></i></b></p>', getDataProcessor().toDataFormat( element.getHtml() ) );
			else
				assert.areSame( '<p class="MsoNormal"><b><i><span lang="EN-US"><o:p>Test</o:p></span></i></b></p>', getDataProcessor().toDataFormat( element.getHtml() ) );
		},

		test_toDataFormat_ticket_3036_1 : function()
		{
			assert.areSame( '<input autocomplete="off" checked="checked" type="checkbox" />',
				getDataProcessor().toDataFormat( '<INPUT type="checkbox" CHECKED  autocomplete=off>' ) );
		},

		test_toDataFormat_ticket_3036_2 : function()
		{
			assert.areSame( '<input autocomplete="off" type="checkbox" unknown="" />',
				getDataProcessor().toDataFormat( '<INPUT type="checkbox" UNKNOWN  autocomplete=off>' ) );
		},

		test_toDataFormat_ticket_2886_1 : function()
		{
			var editor = CKEDITOR.instances.editor1;
			var test = this;
			var isReady = !!editor.dataProcessor;

			if ( !isReady )
			{
				editor.on( 'instanceReady', function()
				{
					isReady = true;
				} );
			}

			this.wait( function()
				{
					if ( !isReady )
					{
						test.wait( arguments.callee, 100 );
						return;
					}

					assert.areSame( '<p>\n\t&nbsp;</p>\n',
						editor.dataProcessor.toDataFormat( '<p></p>' ) );
				}, 100 );
		},

		test_toDataFormat_ticket_2886_2 : function()
		{
			var dataProcessor = CKEDITOR.instances.editor1.dataProcessor;

			var source = '<p>Some text<br><br><br></p>';
			if ( CKEDITOR.env.ie )
				source = '<p>Some text<br><br></p>';
			assert.areSame( '<p>\n\tSome text<br />\n\t<br />\n\t&nbsp;</p>\n',
				dataProcessor.toDataFormat( source ) );
		},

		test_toDataFormat_ticket_2886_3 : function()
		{
			var dataProcessor = CKEDITOR.instances.editor1.dataProcessor;

			assert.areSame( '<p>\n\tSome text<br />\n\t<br />\n\t<br />\n\tSome more text</p>\n',
				dataProcessor.toDataFormat( '<p>Some text<br><br><br>Some more text</p>' ) );
		},

		test_toDataFormat_ticket_2886_4 : function()
		{
			var dataProcessor = CKEDITOR.instances.editor1.dataProcessor;

			assert.areSame( '<p>\n\tSome text<br />\n\t<br />\n\t&nbsp;</p>\n',
				dataProcessor.toDataFormat( '<p>Some text<br><br>&nbsp;</p>' ) );
		},

		test_toDataFormat_ticket_2886_5 : function()
		{
			if ( CKEDITOR.env.ie )
				return;

			var dataProcessor = CKEDITOR.instances.editor1.dataProcessor;

			assert.areSame( '<p>\n\t&nbsp;</p>\n',
				dataProcessor.toDataFormat( '<p><br></p>' ) );
		},

		test_toDataFormat_ticket_2886_6 : function()
		{
			var dataProcessor = CKEDITOR.instances.editor1.dataProcessor;

			var source = '<p><br><br></p>';
			if ( CKEDITOR.env.ie )
				source = '<p><br></p>';

			assert.areSame( '<p>\n\t<br />\n\t&nbsp;</p>\n',
				dataProcessor.toDataFormat( source ) );
		},

		test_toHtml_ticket_2886_1 : function()
		{
			var dataProcessor = CKEDITOR.instances.editor1.dataProcessor;

			var expected = '<p><br /></p>';
			if ( CKEDITOR.env.ie )
				expected = '<p>\xa0</p>';
			assert.areSame( expected, dataProcessor.toHtml( '<p></p>' ) );
		},

		test_toHtml_ticket_2886_2 : function()
		{
			var dataProcessor = CKEDITOR.instances.editor1.dataProcessor;

			var expected = '<p>Some text<br />Some other text</p>';
			assert.areSame( expected, dataProcessor.toHtml( '<p>Some text<br>Some other text</p>' ) );
		},

		test_toHtml_ticket_2886_3 : function()
		{
			var dataProcessor = CKEDITOR.instances.editor1.dataProcessor;

			var expected = '<p>Some text<br /><br /></p>';
			if ( CKEDITOR.env.ie )
				expected = '<p>Some text<br />\xa0</p>';
			assert.areSame( expected, dataProcessor.toHtml( '<p>Some text<br>&nbsp;</p>' ) );
		},

		test_toHtml_ticket_2886_4 : function()
		{
			var dataProcessor = CKEDITOR.instances.editor1.dataProcessor;

			var expected = '<p>Some text</p>';
			assert.areSame( expected, dataProcessor.toHtml( '<p>Some text<br></p>' ));
		},

		test_ticket_3407 : function()
		{
			var editor = CKEDITOR.instances.editor1,
				dataProcessor = editor.dataProcessor,
				config = editor.config;

			config.protectedSource.push( /<\?[\s\S]*?\?>/g );   // PHP Code
			config.protectedSource.push( /<%[\s\S]*?%>/g );   // ASP Code
			config.protectedSource.push( /(<asp:[^\>]+>[\s|\S]*?<\/asp:[^\>]+>)|(<asp:[^\>]+\/>)/gi );   // ASP.Net Code
			dataProcessor.writer = new CKEDITOR.htmlParser.basicWriter();
			var html = getTextAreaValue( '_TEXTAREA1' );
			var protectedHtml = dataProcessor.toHtml( html );
			assert.areSame( html , dataProcessor.toDataFormat( protectedHtml ) );
		},

		test_ticket_3591 : function()
		{
			var editor = CKEDITOR.instances.editor1,
				dataProcessor = editor.dataProcessor;

			dataProcessor.writer = new CKEDITOR.htmlParser.basicWriter();
			var html = getTextAreaValue( '_TEXTAREA_3591' );
			var protectedHtml = dataProcessor.toHtml( html );

			assert.areSame( getTextAreaValue( CKEDITOR.env.ie ? '_TEXTAREA_3591_protected_ie' : '_TEXTAREA_3591_protected' ),
				protectedHtml );
			assert.areSame( getTextAreaValue( '_TEXTAREA_3591' ),
				dataProcessor.toDataFormat( protectedHtml ) );
		},

		test_ticket_3591_2 : function()
		{
			var editor = CKEDITOR.instances.editor1,
				dataProcessor = editor.dataProcessor;

			dataProcessor.writer = new CKEDITOR.htmlParser.basicWriter();
			dataProcessor.writer.sortAttributes = true;

			var html = getTextAreaValue( '_TEXTAREA_3591_2' );
			var protectedHtml = dataProcessor.toHtml( html );

			assert.areSame( getTextAreaValue( '_TEXTAREA_3591_2' ),
				dataProcessor.toDataFormat( protectedHtml ) );
		},

		test_ticket_3869_1 : function()
		{
			var editor = CKEDITOR.instances.editor1,
				dataProcessor = editor.dataProcessor;

			dataProcessor.writer = new CKEDITOR.htmlParser.basicWriter();
			var html = getTextAreaValue( '_TEXTAREA_3869_1' );
			var protectedHtml = dataProcessor.toHtml( html );

			assert.areSame( html , dataProcessor.toDataFormat( protectedHtml ) );
		},

		test_ticket_3869_2 : function()
		{
			var editor = CKEDITOR.instances.editor1,
				dataProcessor = editor.dataProcessor,
				config = editor.config;

			config.protectedSource.push( /<\?[\s\S]*?\?>/g );   // PHP Code
			config.protectedSource.push( /<%[\s\S]*?%>/g );   // ASP Code
			config.protectedSource.push( /(<asp:[^\>]+>[\s|\S]*?<\/asp:[^\>]+>)|(<asp:[^\>]+\/>)/gi );   // ASP.Net Code
			config.protectedSource.push(/<gallery[\s\S]*?<\/gallery>/gi );	// custom protected source
			config.protectedSource.push(/<options[\s\S]*?<\/options>/gi );
			dataProcessor.writer = new CKEDITOR.htmlParser.basicWriter();
			var html = getTextAreaValue( '_TEXTAREA_3869_2' );
			var protectedHtml = dataProcessor.toHtml( html );

			assert.areSame( html , dataProcessor.toDataFormat( protectedHtml ) );
		},

		/**
		 * Test empty value attributes.
		 */
		test_ticket_3884 : function()
		{
			var editor = CKEDITOR.instances.editor1,
				dataProcessor = editor.dataProcessor;
			dataProcessor.writer = new CKEDITOR.htmlParser.basicWriter();
			dataProcessor.writer.sortAttributes = true;

			assert.areSame( '<p><a href="" name="">emptylink</a></p>',
				dataProcessor.toDataFormat( dataProcessor.toHtml( '<p><a href="" name="">emptylink</a></p>' ) ) );
		},

		test_innerHtmlComments_ticket_3801 : function()
		{
			var editor = CKEDITOR.instances.editor1,
				dataProcessor = editor.dataProcessor;

			dataProcessor.writer = new CKEDITOR.htmlParser.basicWriter();

			for ( var i = 1; i <= 7; i++ )
			{
				var html = getTextAreaValue( '_TEXTAREA_3801_' + i );
				var protectedHtml = dataProcessor.toHtml( html );

				assert.areSame( getTextAreaValue( '_TEXTAREA_3801_' + i ),
					dataProcessor.toDataFormat( protectedHtml ) );
			}
		},

		name : document.title
	};
})() );

//window.onload = testCase.test_ticket_3884;
	//]]>
	</script>
</head>
<body>
	<textarea id="editor1" class="ckeditor" cols="80" rows="10"></textarea>
	<textarea id="_TEXTAREA1"><script type="text/javascript">alert('>');</script><table><tbody><tr><!--- IE doesn't handle this comment ---><!--[if gte IE 6 ]>
	<![if lt IE 8 ]>
	gIE conditional comments
<![endif]>
<![endif]--><td><%Response.Write(now())%></td><td><asp:control_name id="some_id" runat="server"/></td><td><?php
include ("head.html"); ?></td></tr></tbody></table><noscript>Your browser doesn't support JavaScript</noscript></textarea>
	<textarea id="_TEXTAREA_3591"><object><param /><param /><embed></embed></object></textarea>
	<textarea id="_TEXTAREA_3591_protected"><cke:object><cke:param></cke:param><cke:param></cke:param><cke:embed></cke:embed></cke:object></textarea>
	<textarea id="_TEXTAREA_3591_protected_ie"><?xml:namespace prefix="cke" /><cke:object><cke:param></cke:param><cke:param></cke:param><cke:embed></cke:embed></cke:object></textarea>
 	<textarea id="_TEXTAREA_3801_1"><!-- foo --><p>bar</p></textarea>
 	<textarea id="_TEXTAREA_3801_2"><p><!-- foo -->bar</p></textarea>
 	<textarea id="_TEXTAREA_3801_3"><!-- foo --><p><!-- foo -->bar</p></textarea>
 	<textarea id="_TEXTAREA_3801_4"><p>bar</p><!-- foo --></textarea>
 	<textarea id="_TEXTAREA_3801_5">bar<!-- foo --></textarea>
 	<textarea id="_TEXTAREA_3801_6"><!-- foo -->bar</textarea>
 	<textarea id="_TEXTAREA_3801_7"><p><!-- foo -->bar</p><p><!-- foo -->bar</p></textarea>
	<textarea id="_TEXTAREA_3869_1">#3869<script language="Javascript" type="text/javascript">
	<!--
		alert('-->');
	//-->
	</script></textarea>
	<textarea id="_TEXTAREA_3869_2">#3869&lt;gallery&gt;
	file.jpg
	&lt;options name="test1" value="value1"&gt;&lt;/options&gt;
	&lt;options name="test2" value="value2"&gt;&lt;/options&gt;
	&lt;/gallery&gt;&lt;?
		echo '&lt;script type="text/javascript"&gt;';
		echo '&lt;!--alert("--&gt;");//--&gt;';
		echo '&lt;/script&gt;';
	?&gt;&lt;!--&lt;%Response.Write(now())%&gt;//--&gt;invalid&lt;?
		echo 'bbb&lt;script&gt;
		&lt;!--
		ccc();&lt;!-- &lt;/script&gt;&lt;!-- &lt;/script&gt;
		//--&gt;&lt;/script&gt;
		&lt;script&gt;&lt;!--
		&lt;!--
		ffff();
		//--&gt;&lt;/script&gt;
		ddd';
	?&gt;html</textarea>
	<textarea id="_TEXTAREA_3591_2"><object classid="clsid"><param name="movie" value="movie.swf" /><embed src="movie.swf" type="application/x-shockwave-flash"></embed></object></textarea>
</body>
</html>
